/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/

#include "editsettingsdialog.h"
#include "ui_editsettingsdialog.h"

#include "settings.h"
#include "filedialog.h"

#include <QSettings>
#include <QVariant>
#include <QString>

EditSettingsDialog::EditSettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditSettingsDialog)
{
    ui->setupUi(this);

	resetValues();

	connect(ui->pBtnSelectDataPath, SIGNAL(clicked()), this, SLOT(showDataRootBrowser()));
	connect(ui->pBtnSelectBackupPath, SIGNAL(clicked()), this, SLOT(showBackupRootBrowser()));
	connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(resetValues()));
	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(apply()));

}

EditSettingsDialog::~EditSettingsDialog()
{
    delete ui;
}

void EditSettingsDialog::apply()
{

	QSettings settings;

	settings.setValue(SETTING_DATA_ROOT, QVariant(ui->pDataPath->text()));
	settings.setValue(SETTING_BACKUP_ROOT, QVariant(ui->pBackupPath->text()));
	settings.setValue(SETTING_DO_BACKUPS, QVariant(ui->pMakeBackups->isChecked()));
	settings.setValue(SETTING_NO_OF_BACKUPS, QVariant(ui->pNoOfBackups->value()));

}

void EditSettingsDialog::resetValues()
{
	QSettings settings;

	ui->pDataPath->setText(settings.value(SETTING_DATA_ROOT).toString());
	ui->pBackupPath->setText(settings.value(SETTING_BACKUP_ROOT).toString());

	ui->pMakeBackups->setChecked(settings.value(SETTING_DO_BACKUPS).toBool());
	ui->pNoOfBackups->setValue(settings.value(SETTING_NO_OF_BACKUPS).toInt());

}

void EditSettingsDialog::showDataRootBrowser()
{
	QString filepath = FileDialog::getExistingDirectory(
				this,
				tr("Select where Basket Data shall be stored"),
				ui->pDataPath->text());

	if (!filepath.isEmpty())
		ui->pDataPath->setText(filepath);



}

void EditSettingsDialog::showBackupRootBrowser()
{
	QString filepath = FileDialog::getExistingDirectory(
				this,
				tr("Select where Basket Backups shall be stored"),
				ui->pBackupPath->text());

	if (!filepath.isEmpty())
		ui->pBackupPath->setText(filepath);


}


void EditSettingsDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
